package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.list.IntList;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.CacheManager;
import com.fr.base.file.ConfigManager;
import com.fr.base.print.PrintUtils;
import com.fr.report.ArrayPageSet;
import com.fr.report.Margin;
import com.fr.report.PageSet;
import com.fr.report.PaperSetting;
import com.fr.report.PaperSize;
import com.fr.report.ReportConstants;
import com.fr.report.ReportPage;
import com.fr.report.ReportWebAttr;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.cal.LayerPageReport;
import com.fr.report.core.cal.LayerReport;
import com.fr.report.io.AppExporter;
import com.fr.report.io.CSVExporter;
import com.fr.report.io.ExcelExporter;
import com.fr.report.io.PDFExporter;
import com.fr.report.io.SVGExporter;
import com.fr.report.io.TextExporter;
import com.fr.report.io.WordExporter;
import com.fr.report.web.EmailManager;
import com.fr.util.Utils;
import com.fr.web.Browser;
import com.fr.web.ErrorHandler;
import com.fr.web.cache.WebletFactory;
import com.fr.web.core.ErrorHandlerHelper;
import com.fr.web.core.Service;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.ShowWorkBookPolicy.Policy4Form;
import com.fr.web.core.WebUtils;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DialogService
  implements Service
{
  private static DialogService DIALOG_SERVICE = new DialogService();

  public static DialogService getInstance()
  {
    return DIALOG_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("dialog_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    SessionIDInfor localSessionIDInfor = SessionDealWith.getSessionIDInfor(paramString2);
    if (localSessionIDInfor == null)
    {
      ErrorHandlerHelper.getErrorHandler().error(paramHttpServletRequest, paramHttpServletResponse, "SessionID: \"" + paramString2 + "\" time out.");
      return;
    }
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    localMap.put("sessionID", paramString2);
    if ("dialog_readPageSetupDialog".equalsIgnoreCase(paramString1))
    {
      readPageSetup0(paramString2, localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
    }
    else if ("dialog_readPageSetupTpl".equalsIgnoreCase(paramString1))
    {
      readPageSetupTpl(localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
    }
    else if ("dialog_changePageSetup".equalsIgnoreCase(paramString1))
    {
      dealWithPageSetup(localSessionIDInfor, paramHttpServletRequest);
    }
    else if ("dialog_readPrintServerDialog".equalsIgnoreCase(paramString1))
    {
      readPrintServerDialog(localMap, localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
    }
    else if ("dialog_submitPrintServerDialog".equalsIgnoreCase(paramString1))
    {
      submitPrintServerDialog(localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
    }
    else
    {
      if ("dialog_readEmailDialog".equalsIgnoreCase(paramString1))
        return;
      if ("dialog_writeEmailDialog".equalsIgnoreCase(paramString1))
      {
        writeEmailDialog(localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
      }
      else if ("dialog_submitEmailDialog".equalsIgnoreCase(paramString1))
      {
        submitEmailDialog(localSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse);
      }
      else if ("dialog_acrobatReaderDialog".equalsIgnoreCase(paramString1))
      {
        readAcrobatReaderDialog(localMap, paramHttpServletResponse);
      }
      else if ("dialog_parameters".equalsIgnoreCase(paramString1))
      {
        localSessionIDInfor.applySessionIDInforParameters(paramHttpServletRequest);
        localSessionIDInfor.clearPageSet();
        localSessionIDInfor.clearPrintPreviewPageSet();
      }
    }
  }

  private static void readPrintServerDialog(Map paramMap, SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String[] arrayOfString = paramSessionIDInfor.getServerPrinterNames();
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("<select name='serverPrinter' style='width:360px'>");
    for (int i = 0; i < arrayOfString.length; ++i)
    {
      String str2 = arrayOfString[i];
      String str3 = "";
      if (i == 0)
        str3 = " selected ";
      localStringBuffer.append("<option value='");
      localStringBuffer.append(str2);
      localStringBuffer.append("'");
      localStringBuffer.append(str3);
      localStringBuffer.append(">");
      localStringBuffer.append(str2);
      localStringBuffer.append("</option>");
    }
    localStringBuffer.append("</select>");
    paramMap.put("PrinterServerNameList", localStringBuffer);
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pn");
    if (str1 != null)
      paramMap.put("pageIndex", str1);
    TemplateUtils.dealWithTemplate("/com/fr/web/core/dialog/printServer.html", paramHttpServletResponse, paramMap);
  }

  private static void submitPrintServerDialog(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Object localObject3;
    Object localObject1 = null;
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "range");
    if (str.equals("all"))
    {
      localObject1 = paramSessionIDInfor.getPrintPreviewPageSet4Traversing();
    }
    else if (str.equals("current"))
    {
      int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pageIndex"));
      localObject1 = new ArrayPageSet(new ReportPage[] { paramSessionIDInfor.getPrintPreviewReportPage(i) }, false);
    }
    else if (str.equals("specify"))
    {
      localObject2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "specify");
      localObject3 = new ArrayList();
      IntList localIntList = IntList.toIntListDecimal((String)localObject2, ',');
      localIntList.sort();
      localIntList.removeEqual();
      for (int j = 0; j < localIntList.size(); ++j)
        ((List)localObject3).add(paramSessionIDInfor.getPrintPreviewReportPage(localIntList.get(j)));
      localObject1 = new ArrayPageSet((ReportPage[])((List)localObject3).toArray(new ReportPage[((List)localObject3).size()]), false);
    }
    Object localObject2 = WebUtils.createPrintWriter(paramHttpServletResponse);
    try
    {
      localObject3 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "serverPrinter");
      if (StringUtils.isNotBlank((String)localObject3))
        PrintUtils.print((PageSet)localObject1, false, (String)localObject3);
      else
        PrintUtils.print((PageSet)localObject1, false, null);
      ((PrintWriter)localObject2).print(true);
    }
    catch (PrinterException localPrinterException)
    {
      localPrinterException.printStackTrace();
      ((PrintWriter)localObject2).print(false);
    }
    ((PrintWriter)localObject2).flush();
    ((PrintWriter)localObject2).close();
  }

  private static void readPageSetup0(String paramString, SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    localMap.put("sessionID", paramString);
    localMap.put("reportIndex", new Integer(i));
    TemplateUtils.dealWithTemplate("/com/fr/web/core/dialog/pageSetup.html", paramHttpServletResponse, localMap);
  }

  private static void readPageSetupTpl(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/core/dialog/pageSetup.cpt", new ShowWorkBookPolicy.Policy4Form(), createPageParameterMap(paramSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse)));
  }

  private static Map createPageParameterMap(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    PaperSetting localPaperSetting = paramSessionIDInfor.getPagerSetting(i);
    if (localPaperSetting == null)
      return localHashMap;
    localHashMap.put("reportIndex", new Integer(i));
    int j = localPaperSetting.getOrientation();
    localHashMap.put("portrait", new Boolean(false));
    localHashMap.put("landscape", new Boolean(false));
    Locale localLocale = FRContext.getLocale();
    PaperSize localPaperSize = localPaperSetting.getPaperSize();
    int k = 1;
    for (int l = 0; l < ReportConstants.PaperSizeNameSizeArray.length; ++l)
    {
      Object[] arrayOfObject = ReportConstants.PaperSizeNameSizeArray[l];
      if (ComparatorUtils.equals(localPaperSize, arrayOfObject[1]))
      {
        String str = (String)arrayOfObject[0];
        localHashMap.put("paperSize", str);
        k = 0;
        break;
      }
    }
    if (ComparatorUtils.equals(Locale.CHINA, localLocale))
    {
      localHashMap.put("user_width", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(localPaperSize.getWidth())));
      localHashMap.put("user_height", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(localPaperSize.getHeight())));
    }
    else
    {
      localHashMap.put("user_width", Utils.doubleToString(localPaperSize.getWidth()));
      localHashMap.put("user_height", Utils.doubleToString(localPaperSize.getHeight()));
    }
    localHashMap.put("predefined", new Boolean(false));
    localHashMap.put("customed", new Boolean(false));
    Margin localMargin = localPaperSetting.getMargin();
    double d1 = localMargin.getLeft();
    double d2 = localMargin.getTop();
    double d3 = localMargin.getRight();
    double d4 = localMargin.getBottom();
    if (ComparatorUtils.equals(Locale.CHINA, localLocale))
    {
      localHashMap.put("margin_top", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(d2)));
      localHashMap.put("margin_left", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(d1)));
      localHashMap.put("margin_bottom", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(d4)));
      localHashMap.put("margin_right", Utils.doubleToString(BaseCoreUtils.convertInchesToMillimeters(d3)));
    }
    else
    {
      localHashMap.put("margin_top", Utils.doubleToString(d2));
      localHashMap.put("margin_left", Utils.doubleToString(d1));
      localHashMap.put("margin_bottom", Utils.doubleToString(d4));
      localHashMap.put("margin_right", Utils.doubleToString(d3));
    }
    return localHashMap;
  }

  private static void dealWithPageSetup(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    Locale localLocale = FRContext.getLocale();
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__parameters__");
    JSONObject localJSONObject = new JSONObject(str1);
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    PaperSetting localPaperSetting = new PaperSetting();
    boolean bool1 = localJSONObject.getBoolean("portrait");
    localPaperSetting.setOrientation((bool1) ? 0 : 1);
    boolean bool2 = localJSONObject.getBoolean("predefined");
    if (bool2)
    {
      String str2 = localJSONObject.getString("paperSize");
      int j = 0;
      while (true)
      {
        if (j >= ReportConstants.PaperSizeNameSizeArray.length)
          break label222;
        Object[] arrayOfObject = ReportConstants.PaperSizeNameSizeArray[j];
        if (ComparatorUtils.equals(str2, arrayOfObject[0]))
        {
          localPaperSetting.setPaperSize((PaperSize)arrayOfObject[1]);
          break label222:
        }
        ++j;
      }
    }
    double d1 = localJSONObject.getDouble("user_width");
    double d2 = localJSONObject.getDouble("user_height");
    PaperSize localPaperSize = new PaperSize();
    if (ComparatorUtils.equals(Locale.CHINA, localLocale))
    {
      localPaperSize.setWidth(BaseCoreUtils.convertMillimetersToInches(d1));
      localPaperSize.setHeight(BaseCoreUtils.convertMillimetersToInches(d2));
    }
    else
    {
      localPaperSize.setWidth(d1);
      localPaperSize.setHeight(d2);
    }
    localPaperSetting.setPaperSize(localPaperSize);
    label222: d1 = localJSONObject.getDouble("margin_top");
    d2 = localJSONObject.getDouble("margin_left");
    double d3 = localJSONObject.getDouble("margin_bottom");
    double d4 = localJSONObject.getDouble("margin_right");
    Margin localMargin = new Margin();
    if (ComparatorUtils.equals(Locale.CHINA, localLocale))
    {
      localMargin.setTop(BaseCoreUtils.convertMillimetersToInches(d1));
      localMargin.setLeft(BaseCoreUtils.convertMillimetersToInches(d2));
      localMargin.setBottom(BaseCoreUtils.convertMillimetersToInches(d3));
      localMargin.setRight(BaseCoreUtils.convertMillimetersToInches(d4));
    }
    else
    {
      localMargin.setTop(d1);
      localMargin.setLeft(d2);
      localMargin.setBottom(d3);
      localMargin.setRight(d4);
    }
    localPaperSetting.setMargin(localMargin);
    paramSessionIDInfor.setPaperSetting(i, localPaperSetting);
  }

  private static void writeEmailDialog(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    HashMap localHashMap = new HashMap();
    EmailManager localEmailManager = paramSessionIDInfor.getEmailManager();
    if (localEmailManager != null)
      localHashMap.put("fromAddress", localEmailManager.getFromEmailAddress());
    localHashMap.put("parentSessionID", paramSessionIDInfor.getSessionID());
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/core/dialog/email.cpt", new ShowWorkBookPolicy.Policy4Form(), localHashMap));
  }

  private static void submitEmailDialog(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ConfigManager localConfigManager = FRContext.getConfigManager();
    String str1 = paramSessionIDInfor.getReportletTitle().replaceAll("\\s", "_");
    str1 = Browser.resolve(paramHttpServletRequest).getEncodedFileName4Download(str1);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str2 = "";
    String str3 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "format");
    if (str3.equalsIgnoreCase("PDF"))
    {
      str2 = "pdf";
    }
    else if (str3.equalsIgnoreCase("Excel"))
    {
      localObject1 = paramSessionIDInfor.getWorkBook2Show();
      i = 0;
      for (int j = 0; j < ((ResultWorkBook)localObject1).getReportCount(); ++j)
        if ((((ResultWorkBook)localObject1).getReport(j) instanceof LayerReport) || (((ResultWorkBook)localObject1).getReport(j) instanceof LayerPageReport))
        {
          i = 1;
          break;
        }
      str2 = (i != 0) ? "zip" : "xls";
    }
    else if (str3.equalsIgnoreCase("SVG"))
    {
      str2 = "svg";
    }
    else if (str3.equalsIgnoreCase("HTML"))
    {
      str2 = "zip";
    }
    else if (str3.equalsIgnoreCase("CSV"))
    {
      str2 = "csv";
    }
    else if (str3.equalsIgnoreCase("Text"))
    {
      str2 = "txt";
    }
    else if (str3.equalsIgnoreCase("Word"))
    {
      str2 = "doc";
    }
    Object localObject1 = new File(FRContext.getCacheManager().getCacheDirectory(), "_" + System.currentTimeMillis() + File.separator + str1 + "." + str2);
    for (int i = 0; ((File)localObject1).exists(); ++i)
      localObject1 = new File(FRContext.getCacheManager().getCacheDirectory(), "_" + System.currentTimeMillis() + File.separator + str1 + "_" + i + "." + str2);
    Utils.mkdirs(((File)localObject1).getParentFile());
    FileOutputStream localFileOutputStream = new FileOutputStream((File)localObject1);
    Object localObject2 = null;
    if (str3.equalsIgnoreCase("PDF"))
    {
      localObject2 = new PDFExporter();
    }
    else if (str3.equalsIgnoreCase("Excel"))
    {
      localObject2 = new ExcelExporter(null);
    }
    else if (str3.equalsIgnoreCase("SVG"))
    {
      localObject2 = new SVGExporter();
    }
    else if (str3.equalsIgnoreCase("CSV"))
    {
      localObject2 = new CSVExporter();
    }
    else if (str3.equalsIgnoreCase("Text"))
    {
      localObject2 = new TextExporter();
    }
    else if (str3.equalsIgnoreCase("Word"))
    {
      localObject2 = new WordExporter();
    }
    else
    {
      ErrorHandlerHelper.getErrorHandler().error(paramHttpServletRequest, paramHttpServletResponse, "Cannot recognize the specifed export format:" + str3);
      return;
    }
    try
    {
      if (localObject2 != null)
        if (localObject2 instanceof ExcelExporter)
        {
          ((ExcelExporter)localObject2).export(localFileOutputStream, paramSessionIDInfor.getWorkBook2Show(), true);
        }
        else
        {
          PageSet localPageSet = paramSessionIDInfor.getPrintPreviewPageSet4Traversing();
          ((AppExporter)localObject2).export(localFileOutputStream, localPageSet);
        }
      ((File)localObject1).deleteOnExit();
      ((File)localObject1).getParentFile().deleteOnExit();
    }
    catch (Exception localException1)
    {
      FRContext.getLogger().log(Level.WARNING, localException1.getMessage(), localException1);
      localPrintWriter.print("false");
      localPrintWriter.flush();
      localPrintWriter.close();
      return;
    }
    try
    {
      ReportWebAttr localReportWebAttr = localConfigManager.getReportWebAttr();
      EmailManager localEmailManager = localReportWebAttr.getEmailManager();
      if (localEmailManager != null)
      {
        localEmailManager.send(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "to"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "cc"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "bcc"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "from"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "subject"), WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "message"), localObject1);
        localPrintWriter.print("true");
      }
      else
      {
        localPrintWriter.print("EmailManager is null");
      }
    }
    catch (Exception localException2)
    {
      FRContext.getLogger().log(Level.WARNING, localException2.getMessage(), localException2);
      localPrintWriter.print("false");
    }
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void readAcrobatReaderDialog(Map paramMap, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF8");
    TemplateUtils.dealWithTemplate("/com/fr/web/core/dialog/loadAcrobatReader.html", localPrintWriter, paramMap);
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}